package com.my.jwt.shiro.test1;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.louislivi.fastdep.shirojwt.FastDepShiroJwtProperties;
import org.apache.shiro.SecurityUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 还可以重写JwtUtil类实现token的校验生成等。
 * 可以不要
 * @author timo 2021/3/19
 */
@ConditionalOnMissingBean(JwtUtil.class)
public class JwtUtil {
    @Resource
    public FastDepShiroJwtProperties fastDepShiroJwtProperties;
    @Resource
    public FastDepShiroJwtAuthorization fastDepShiroJwtAuthorization;

    /**
     * 校验token
     *
     * @param token token
     * @return true or false
     */
    public boolean verify(String token, String userId) {
        try {
            String secret = fastDepShiroJwtAuthorization.getSecret(userId) == null ? fastDepShiroJwtProperties.getSecret() : null;
            assert secret != null;
            Algorithm algorithm = Algorithm.HMAC256(secret);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withSubject(userId)
                    .build();
            verifier.verify(token);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 获取当前用户id
     *
     * @return user id
     */
    public String getUserId() {
        try {
            DecodedJWT jwt = JWT.decode(SecurityUtils.getSubject().getPrincipal().toString());
            return jwt.getSubject();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据token获取用户id
     *
     * @param token jwt token
     * @return user id
     */
    public String getUserId(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getSubject();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 生成token
     *
     * @param userId user identifier
     * @return token
     */
    public String sign(String userId) {
        Date date = new Date(System.currentTimeMillis() + fastDepShiroJwtProperties.getExpireTime());
        String secret = fastDepShiroJwtAuthorization.getSecret(userId) == null ? fastDepShiroJwtProperties.getSecret() : null;
        assert secret != null;
        Algorithm algorithm = Algorithm.HMAC256(secret);
        return fastDepShiroJwtProperties.getSignPrefix() + JWT.create()
                .withSubject(userId)
                .withExpiresAt(date)
                .sign(algorithm);
    }
}
